package com.qf.text.utilstext;



import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * @author: lch
 * @Date:2023/4/12
 */
@SpringBootTest(classes= MessageCodeText.class)
@RunWith(SpringRunner.class)
public class MD5Text {

    @Test
    public void str2MD5() {
        StringBuilder sb = new StringBuilder();
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update("123456".getBytes(StandardCharsets.UTF_8));
            byte[] b = md.digest();
            int d;
            for (byte value : b) {
                d = value;
                if (d < 0) {
                    d = value & 0xff;
                    // 与上一行效果等同
                    // i += 256;
                }
                if (d < 16)
                    sb.append("0");
                sb.append(Integer.toHexString(d));
            }
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        System.out.println(sb.toString());
    }

}
